class Solution {
public:
    int deleteAndEarn(vector<int>& nums) {
        sort(nums.begin(),nums.end());
        int max_x = nums[nums.size()-1];
        vector<int> arr(max_x+1);
        for(auto e:nums)
        {
            arr[e]+=e;
        }
        vector<int> f(arr.size());
        vector<int>g(arr.size());
        f[0]=arr[0];
        for(int i=1;i<arr.size();i++)
        {
            f[i] = arr[i]+g[i-1];
            g[i] = max(f[i-1],g[i-1]);
        }
        return max(f[arr.size()-1],g[arr.size()-1]);

    }
};